#include "Session.h"
#include "Market.h"
#include "Player.h"
#include <string>
#include <iostream>
#include <map>

using namespace std;

string Session::state_names[5] = {"VIC","NSW","SA","QLD","WA"};

Session::Session(void)
{
	day = 1;
	max_days = 30;
	running = true;
	total_states = 5;
	//cout << "Session constructor - s[0]: "  << state_names[0] << endl;
}

void Session::print_state_names(void) { cout << "PRINTING STATE_NAME[0]: " << state_names[0] << endl;}

int Session::get_total_states(void) { return total_states; }

void Session::start_game(void)
{
	string test_char;
	cout << "Started game (y)es or (n)o:"<< endl;
	cin >> test_char;
	if (test_char[0] == 'y')
	{
		init_player();
		init_market();		
		init_states();
		current_state_index = 0;
		days_choice();
		for (day;day<=max_days;day++)
		{
		// initialise states 
		// for each state initialise a market
			int day_choice = day_menu();
			if (day_choice == 1)
			{
				// change city
				change_city();
			}
			else
			{
				// trade
				trade();
			}
		// Menu
		// Market rates 
		// Travel options
		// Specific state option

		//Market new_market = Market::Market();
		}
	}
	else { running = false; }
}

void Session::trade(void)
{
	// display market for that state
	cout << "---------- Trading --------------" << endl;
	cout << current_state_index << endl;
	cout << states[current_state_index].get_name() << endl;
	states[current_state_index].print_market();
}


int Session::day_menu(void)
{
	int choice;
	cout << "---------------------------" << endl;
	cout << "Day: " << day << endl;
	cout << "City: " << current_state_name() << endl; 
	cout << "---------------------------" << endl;
	cout << "1)  - Change city" << endl; 
	cout << "2)  - Trade in this city" << endl;
	cin >> choice;
	if (choice == 1)
	{
		return choice;
	} else { return 2; } 
}

void Session::change_city(void)
{
	cout << "Choose city:" << endl;
	int city_choice;
	for (int x=0; x < get_total_states(); x++)
	{
		cout << x << ")  - " << states[x].get_name() << endl;
	}
	cin >> city_choice;
	switch (city_choice)
		{
		case 0: 
			{
				cout << "Moving to " << states[0].get_name() << endl;
				current_state_index = 0;
				break;
			}
		case 1: 
			{
				cout << "Moving to " << states[1].get_name() << endl;
				current_state_index = 1;
				break;
			}
		case 2: 
			{
				cout << "Moving to " << states[2].get_name() << endl;
				current_state_index = 2;
				break;
			}
		case 3: 
			{
				cout << "Moving to " << states[3].get_name() << endl;
				current_state_index = 3;
				break;
			}
		case 4: 
			{
				cout << "Moving to " << states[4].get_name() << endl;
				current_state_index = 4;
				break;
			}

		default: 
			{
				cout << "Not a valid input... try again" << endl;
				change_city();
				break;
			}
		}
}

void Session::days_choice(void)
{
	int days;
	cout << "How many days do you want the game to go for?" << endl;
	while(! (cin >> days) )
	{
		cout << " This is not a number. Enter the amount of days you want:" << endl;
		cin.clear();
		cin.ignore(1000, '\n');
	}
	set_max_days(days);
	days_print();
}

void Session::set_max_days(int x) { max_days = x; }

void Session::days_print() { cout << "Start dealing for " << max_days << " days!" << endl; }

void Session::init_player()
{
	string cin_player_name;
	cout << "Players name: ";
	cin >> cin_player_name;
	Player new_player = Player(cin_player_name);
	current_player = new_player;
}
void Session::init_market()
{
	/*  This is where we set the market prices */
	cout << "initialise market..." << endl;
	Session::set_market["Car"] = 50000.0;
	Session::set_market["TV"] = 950.0;
	Session::set_market["Computer"] = 1200.0;
	Session::set_market["Shoes"] = 150.0;
	Session::set_market["Watches"] = 100.0;
	Session::set_market["Stereo"] = 23.50;
	Session::set_market["Motorbike"] = 18750.0;
	Session::set_market["Coffee machine"] = 210.0;
}

void Session::init_states(void)
{
	map<string,float>* pSetMarket = &set_market;
	//cout << "SM Pointer: " << pSetMarket << endl;
	//cout << "INITIALIZING STATES:" << endl;
	for (int x=0; x < get_total_states(); x++)
	{
		//cout << "Initializing " << state_names[x] << endl;
		State new_state = State(state_names[x]);
		new_state.set_local_market(pSetMarket);
		states[x] = new_state; 
	}
}




void Session::print_states(void)
{
	//cout << "printing states:" << endl;
	for (int y=0; y < get_total_states(); y++)
	{
		cout << "State:" <<  states[y].get_name() << endl;
		//cout << "State: " << states[y].get_name() << endl;
	}
}

string Session::current_state_name(void)
{
	return states[current_state_index].get_name();
}

Session::~Session(void)
{
}
